#include "GfxStats.h"

GfxStats::GfxStats() : m_pFont(NULL)
{
	m_vertexCount			= 0;
	m_triangleCount			= 0;
	mFPS					= 0.0f;
	mSecondsPerFrame		= 0.0f;
	mMilliSecsPerFrame		= 0.0f;
	m_pFont					= new cFont();
}

GfxStats::~GfxStats()
{
	delete m_pFont;
}

void GfxStats::addTriangles(DWORD count) 
{
	m_triangleCount += count;
}

void GfxStats::addVertices(DWORD count)
{
	m_vertexCount	+= count;
}

void GfxStats::subTriangles(DWORD count)
{
	m_triangleCount -= count;
}

void GfxStats::subVertices(DWORD count)
{
	m_vertexCount -= count;
}

void GfxStats::setTriCount(DWORD count)
{
	m_triangleCount = count;
}

void GfxStats::setVertexCount(DWORD count)
{
	m_vertexCount = count;
}

void GfxStats::update(float dt)
{
	//Statically binds the 
	static float timeElapsed	= 0.0f;

	static float numberOfFrames	= 0;

	timeElapsed					+= dt;

	numberOfFrames++;

	if(timeElapsed >= 1.0f) {
		//
		mFPS				= numberOfFrames;

		mSecondsPerFrame	= 1.0f / numberOfFrames;

		//Provides the conversion of the amount of frames passed
		//per Milli
		mMilliSecsPerFrame	= 1000.0f / numberOfFrames;

		timeElapsed			= 0.0f;
		numberOfFrames		= 0;
	}
}

void GfxStats::displayStats()
{
	//Make static so memory is not allocated every frame
	static char buffer[256];

	sprintf_s(buffer, "Frames Per Second = %.2f\n"
					"Seconds Per Frame = %.8f\n"
					"Milliseconds Per Frame = %.4f\n"
					"Triangle Count = %d\n"
					"Vertex Count = %d\n", 
					mFPS, mSecondsPerFrame, mMilliSecsPerFrame, 
					m_triangleCount, m_vertexCount);

	RECT rect = { 5, 5, 0, 0 };

	m_pFont->drawText(buffer, rect);
}